<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="150px"
      v-loading="formLoading"
    >
<!--      <el-form-item label="角色id" prop="roleId">-->
<!--        <el-input v-model="formData.roleId" placeholder="请输入角色id" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="消息发送者类型" prop="senderType">-->
<!--        <el-select v-model="formData.senderType" placeholder="请选择消息发送者类型">-->
<!--          <el-option label="AI" :value="2" />-->
<!--          <el-option label="用户" :value="1" />-->
<!--          <el-option label="系统" :value="3" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="用户消息" prop="userText">
        <el-input v-model="formData.userText" placeholder="请输入用户消息" />
      </el-form-item>
<!--      <el-form-item label="用户消息hash" prop="userTextHash">-->
<!--        <el-input v-model="formData.userTextHash" placeholder="请输入用户消息hash" />-->
<!--      </el-form-item>-->
      <el-form-item label="回复消息" prop="replyText">
        <el-input v-model="formData.replyText" placeholder="请输入回复消息" />
      </el-form-item>
<!--      <el-form-item label="回复消息hash" prop="replyTextHash">-->
<!--        <el-input v-model="formData.replyTextHash" placeholder="请输入回复消息hash" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="消息类型" prop="contentType">-->
<!--        <el-select v-model="formData.contentType" placeholder="请选择消息类型">-->
<!--          <el-option label="文本/音频" :value="1" />-->
<!--          <el-option label="图片" :value="2" />-->
<!--          <el-option label="视频" :value="3" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="音频数据" prop="audioData">-->
<!--        <el-input v-model="formData.audioData" placeholder="请输入音频数据" />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="扩展" prop="extension">-->
<!--        <el-input v-model="formData.extension" placeholder="请输入扩展" />-->
<!--      </el-form-item>-->
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { MessageCollectionApi, MessageCollectionVO } from '@/api/lm/messagecollection'

/** 消息仓库 表单 */
defineOptions({ name: 'MessageCollectionForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  messageId: undefined,
  roleId: undefined,
  senderType: undefined,
  userText: undefined,
  userTextHash: undefined,
  replyText: undefined,
  replyTextHash: undefined,
  contentType: undefined,
  audioData: undefined,
  extension: undefined
})
const formRules = reactive({
  roleId: [{ required: true, message: '角色id不能为空', trigger: 'blur' }],
  senderType: [{ required: true, message: '消息发送者类型, 1: 用户，2: AI, 3: 系统不能为空', trigger: 'change' }],
  userText: [{ required: true, message: '用户消息不能为空', trigger: 'blur' }],
  userTextHash: [{ required: true, message: '用户消息hash不能为空', trigger: 'blur' }],
  contentType: [{ required: true, message: '消息类型, 1: 文本/音频, 2: 图片, 3: 视频不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await MessageCollectionApi.getMessageCollection(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as MessageCollectionVO
    if (formType.value === 'create') {
      await MessageCollectionApi.createMessageCollection(data)
      message.success(t('common.createSuccess'))
    } else {
      await MessageCollectionApi.updateMessageCollection(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    messageId: undefined,
    roleId: undefined,
    senderType: undefined,
    userText: undefined,
    userTextHash: undefined,
    replyText: undefined,
    replyTextHash: undefined,
    contentType: undefined,
    audioData: undefined,
    extension: undefined
  }
  formRef.value?.resetFields()
}
</script>
